﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy5_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text = "圆柱体和圆锥体";
            this.Size = new Size(300, 300); // 设置窗体大小
            this.BackColor = Color.Yellow; // 设置窗体背景为黄色（匹配运行界面）
            this.Paint += Form1_Paint; // 绑定Paint事件
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias; // 抗锯齿，让图形更平滑


            // ========== 1. 绘制圆柱体 ==========
            // 圆柱体的坐标平移：以(40,40)为基准
            Matrix cylinderMatrix = new Matrix();
            cylinderMatrix.Translate(40, 40); // 平移到(40,40)
            g.Transform = cylinderMatrix; // 应用平移变换

            // 定义圆柱体的矩形
            Rectangle cylinderTopRect = new Rectangle(0, 0, 80, 40); // 顶部椭圆外切矩形
            Rectangle cylinderBottomRect = new Rectangle(0, 140, 80, 40); // 底部椭圆外切矩形（下移140）
            Rectangle cylinderSideRect = new Rectangle(0, 0, 80, 140); // 侧面矩形

            // ① 绘制圆柱体侧面（黑到白的线性渐变）
            LinearGradientBrush sideBrush = new LinearGradientBrush(
                cylinderSideRect,
                Color.Black,
                Color.White,
                LinearGradientMode.Vertical // 垂直渐变
            );
            g.FillRectangle(sideBrush, cylinderSideRect);
            g.DrawRectangle(Pens.Black, cylinderSideRect); // 侧面边框

            // ② 绘制圆柱体顶部椭圆
            g.DrawEllipse(Pens.Black, cylinderTopRect);

            // ③ 绘制圆柱体底部椭圆（白到黑的线性渐变）
            LinearGradientBrush bottomBrush = new LinearGradientBrush(
                cylinderBottomRect,
                Color.White,
                Color.Black,
                LinearGradientMode.Vertical
            );
            g.FillEllipse(bottomBrush, cylinderBottomRect);
            g.DrawEllipse(Pens.Black, cylinderBottomRect);


            // ========== 2. 绘制圆锥体 ==========
            // 圆锥体的坐标平移：在圆柱体基础上再右移120
            Matrix coneMatrix = new Matrix();
            coneMatrix.Translate(40 + 120, 40); // 平移到(40+120, 40)
            g.Transform = coneMatrix; // 应用平移变换

            // 定义圆锥体的矩形和顶点
            Rectangle coneBottomRect = new Rectangle(0, 140, 80, 40); // 底部椭圆（与圆柱体底部同位置）
            Point[] coneSidePoints = new Point[] { // 侧面三角形顶点
                new Point(40, 0), // 顶部顶点（与圆柱体顶部椭圆最高点重合）
                new Point(0, 140), // 底部左端点
                new Point(80, 140) // 底部右端点
            };

            // ① 绘制圆锥体侧面（黑到白的线性渐变）
            LinearGradientBrush coneSideBrush = new LinearGradientBrush(
                new Rectangle(0, 0, 80, 140),
                Color.Black,
                Color.White,
                LinearGradientMode.Vertical
            );
            g.FillPolygon(coneSideBrush, coneSidePoints);
            g.DrawPolygon(Pens.Black, coneSidePoints); // 侧面边框

            // ② 绘制圆锥体底部椭圆（黑到白的线性渐变）
            LinearGradientBrush coneBottomBrush = new LinearGradientBrush(
                coneBottomRect,
                Color.Black,
                Color.White,
                LinearGradientMode.Vertical
            );
            g.FillEllipse(coneBottomBrush, coneBottomRect);
            g.DrawEllipse(Pens.Black, coneBottomRect);


            // 释放资源
            sideBrush.Dispose();
            bottomBrush.Dispose();
            coneSideBrush.Dispose();
            coneBottomBrush.Dispose();
            g.ResetTransform(); // 重置坐标变换
        }
    }
}
